<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	// $sre=M('question')->select();
    	//主问题循环
    	$sql='select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id order by t1.stid desc,t1.create_time desc';
    	$sre=M()->query($sql);
    	for ($i=0; $i <count($sre) ; $i++) { 
    		$sre[$i]['create_time']=getFormate($sre[$i]['create_time']);
    	}
    	// var_dump($src);
        // 排行
        $time = strtotime('-30 days');
        $sql ="select count(t1.id) cn,t2.face,t2.nikename,t2.id from answer t1 left join user t2 on t1.uid=t2.id where t1.create_time>'{$time}' group by t1.uid order by cn desc limit 12";
        $arr=M()->query($sql);
        $list = M('question')->where("create_time>'{$time}'")->order('view_num desc')->limit(10)->select();
        $ans = M('question')->where("create_time>'{$time}'")->order('answer_num desc')->limit(10)->select();
        // var_dump($arr);
        // exit;
        $this->assign('list',$list);
        $this->assign('ans',$ans);
    	$this->assign('sre',$sre);
        $this->assign('arr',$arr);
        $this->display();
    }
    // 全部
    public function all()
    {
        $count      = M('question')->count();
        // 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        // var_dump($Page->firstRow);第几条开始取
        // var_dump($Page->listRows);取几条

        // echo($show);exit;

    	$sql='select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id order by stid desc,t1.create_time desc limit '.$Page->firstRow.",".$Page->listRows;
    	$sre=M()->query($sql);
    	for ($i=0; $i <count($sre) ; $i++) { 
    		$sre[$i]['create_time']=getFormate($sre[$i]['create_time']);
    	}
    	// var_dump($src);
        $this->assign('page',$show);
    	$this->assign('sre',$sre);
        $this->display();
    }
    // 未解决
    public function not()
    {
        $sql = "select * from question where find_in_set('未解决',status)";
        
        $count = M()->query($sql);
        $count = count($count);
        
        // $count      = M('question')->where(find_in_set("'未解决',question"))->count();
        // 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();
		$sql="select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id where find_in_set('未解决',t1.status) order by stid desc,t1.create_time desc limit ".$Page->firstRow.",".$Page->listRows;
    	$sre=M()->query($sql);
    	for ($i=0; $i <count($sre) ; $i++) { 
    		$sre[$i]['create_time']=getFormate($sre[$i]['create_time']);
    	}
    	// var_dump($src);
        $this->assign('page',$show);
    	$this->assign('sre',$sre);
        $this->display();

    }
    // 已解决
    public function resolved()
    {
         $sql = "select * from question where find_in_set('未解决',status)";
        
        $count = M()->query($sql);
        $count = count($count);
        
        // $count      = M('question')->where(find_in_set("'已解决',question"))->count();
        // 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();
    	$sql="select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id where find_in_set('已解决',t1.status) order by stid desc,t1.create_time desc limit ".$Page->firstRow.",".$Page->listRows;
    	$sre=M()->query($sql);
    	for ($i=0; $i <count($sre) ; $i++) { 
    		$sre[$i]['create_time']=getFormate($sre[$i]['create_time']);
    	}
    	// var_dump($src);
        $this->assign('page',$show);
    	$this->assign('sre',$sre);
        $this->display();

    }
    // 精华
    public function elite()
    {
         $sql = "select * from question where find_in_set('未解决',status)";
        
        $count = M()->query($sql);
        $count = count($count);
        
        // 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();
    	$sql="select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id where find_in_set('精华',t1.status) order by stid desc,t1.create_time desc limit ".$Page->firstRow.",".$Page->listRows;
    	$sre=M()->query($sql);
    	for ($i=0; $i <count($sre) ; $i++) { 
    		$sre[$i]['create_time']=getFormate($sre[$i]['create_time']);
    	}
    	// var_dump($src);
        $this->assign('page',$show);
    	$this->assign('sre',$sre);
        $this->display();

    }
    // 搜索
    public function hunt()
    {
        $data = I('get.');
        
        $data = seek($data['q']);
        // 
        // $data = count($data);
        $sql = "select * from question where ";
        foreach ($data as $v) {
            $sql .="title like '%".$v['word']."%' or ";
        }
        $sql = rtrim($sql,'or ');
        $sql .='order by stid desc';
        $res = M()->query($sql);
        foreach ($res as $v) {
            $sql="select t1.*,t2.face,t2.nikename,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id where t1.id={$v['id']}";
         $sre[]=M()->query($sql);   
        }
        // $count = count($sre);
        // $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $show       = $Page->show();
        // $res = current($res);
        // $arr = M('user')->field('nikename,face')->where("id={$res['uid']}")->select();
        // var_dump($arr);
        // var_dump($sre);
        // var_dump($data);
        $this->assign('sre',$sre);
        // $this->assign('page',$show);
        $this->display();
    }
    
}
?>